﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mokkula
{
    /// <summary>
    /// Text box that displays an hint to the user in the text box.
    /// When user clicks the text box hint disappears and user can start typing.
    /// </summary>
    public class HintTextBox : TextBox
    {
        bool _showingHint = true;
        string _hint;

        /// <summary>
        /// Construct new text box that shows a hint to the user.
        /// </summary>
        public HintTextBox()
        {
            this.GotFocus += delegate { HideHint(); };
            this.LostFocus += delegate { ShowHint(); };

        }

        /// <summary>
        /// Check if the control is showing the hint text.
        /// </summary>
        public bool ShowingHint
        {
            get { return _showingHint; }
        }

        /// <summary>
        /// Get or set the hint that is shown to the user.
        /// </summary>
        [Localizable(true)]
        [Bindable(true)]
        public string Hint
        {
            get { return _hint; }
            set
            {
                _hint = value;
                ShowHint();
            }
        }

        /// <summary>
        /// Get or set the current text.
        /// </summary>
        public override string Text
        {
            get { return _showingHint ? "" : base.Text; }
            set { base.Text = value; }
        }

        private void HideHint()
        {
            if (_showingHint)
            {
                _showingHint = false;
                Text = "";
                Font = Control.DefaultFont;
                ForeColor = Control.DefaultForeColor;
            }
        }

        private void ShowHint()
        {
            if (base.Text.Length == 0)
            {
                _showingHint = true;
                Text = _hint;
                Font = new Font(Control.DefaultFont.FontFamily, 8.25F, FontStyle.Italic);
                ForeColor = SystemColors.InactiveCaption;
            }
        }
    }
}
